package org.elsfs.framework.generator;

import java.net.Inet4Address;
import java.net.UnknownHostException;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * twitter的snowflake算法 -- java实现 参考 <a
 * href="https://github.com/beyondfengyu/SnowFlake/blob/master/SnowFlake.java"/>
 *
 * @author beyond
 */
public class SnowFlake {

  /** 起始的时间戳 */
  private static final long START_STAMP = 1480166465631L;

  /** 序列号占用的位数 */
  private static final long SEQUENCE_BIT = 12;

  /** 机器标识占用的位数 */
  private static final long MACHINE_BIT = 5;

  /** 数据中心占用的位数 */
  private static final long DATACENTER_BIT = 5;

  /** 最大支持的数据中心数量：31 */
  private static final long MAX_DATACENTER_NUM = ~(-1L << DATACENTER_BIT);

  /** 最大支持的机器数量：31 */
  private static final long MAX_MACHINE_NUM = ~(-1L << MACHINE_BIT);

  private static final long MAX_SEQUENCE = ~(-1L << SEQUENCE_BIT);

  /** 每一部分向左的位移 */
  private static final long MACHINE_LEFT = SEQUENCE_BIT;

  private static final long DATACENTER_LEFT = MACHINE_LEFT + MACHINE_BIT;
  private static final long TIMESTAMP_LEFT = DATACENTER_LEFT + DATACENTER_BIT;

  /** 数据中心 */
  private final long dataCenterId;

  /** 机器标识 */
  private final long machineId;

  /** 序列号 */
  private long sequence = 0L;

  /** 上一次时间戳 */
  private long lastTimestamp = -1L;

  /**
   * 构造方法
   *
   * @param dataCenterId 数据中心ID
   */
  public SnowFlake(long dataCenterId) {
    long machineId = generateMachineId();

    if (dataCenterId > MAX_DATACENTER_NUM || dataCenterId < 0) {
      throw new IllegalArgumentException(
          "dataCenterId can't be greater than MAX_DATACENTER_NUM or less than 0");
    }
    if (machineId > MAX_MACHINE_NUM || machineId < 0) {
      throw new IllegalArgumentException(
          "machineId can't be greater than MAX_MACHINE_NUM or less than 0");
    }
    this.dataCenterId = dataCenterId;
    this.machineId = machineId;
  }

  /**
   * 产生下一个ID
   *
   * @return id
   */
  public synchronized long nextId() {
    long currentStamp = getNewTimestamp();
    if (currentStamp < lastTimestamp) {
      throw new RuntimeException("Clock moved backwards.  Refusing to generate id");
    }

    if (currentStamp == lastTimestamp) {
      // 相同毫秒内，序列号自增
      sequence = (sequence + 1) & MAX_SEQUENCE;
      // 同一毫秒的序列数已经达到最大
      if (sequence == 0L) {
        currentStamp = getNextMill();
      }
    } else {
      // 不同毫秒内，序列号置为0
      sequence = 0L;
    }

    lastTimestamp = currentStamp;

    return (currentStamp - START_STAMP) << TIMESTAMP_LEFT
        | dataCenterId << DATACENTER_LEFT
        | machineId << MACHINE_LEFT
        | sequence;
  }

  private long getNextMill() {
    long mill = getNewTimestamp();
    while (mill <= lastTimestamp) {
      mill = getNewTimestamp();
    }
    return mill;
  }

  /**
   * 获取当前时间戳
   *
   * @return 时间戳
   */
  private long getNewTimestamp() {
    return System.currentTimeMillis();
  }

  private static Long generateMachineId() {
    try {
      String hostAddress = Inet4Address.getLocalHost().getHostAddress();
      int[] ints = StringUtils.toCodePoints(hostAddress);
      int sums = 0;
      for (int b : ints) {
        sums += b;
      }
      return (long) (sums % 32);
    } catch (UnknownHostException e) {
      // 如果获取失败，则使用随机数备用
      return RandomUtils.nextLong(0, 31);
    }
  }
}
